// 系统管理模块结构体定义

package models

import (
	"api/utils"
	"time"

	"gorm.io/gorm"
)

// Logger 访问日志
type Logger struct {
	Id        uint32 `json:"id"`
	UserId    uint32 `json:"userId" gorm:"column:userId;default:0;comment:用户id"`
	Ip        string `json:"ip" gorm:"size:15;default:0.0.0.0;comment:客户端ip地址"`
	UserAgent string `json:"userAgent" gorm:"column:userAgent;size:255;comment:浏览器信息"`
	Path      string `json:"path" gorm:"size:50;comment:访问路径"`
	Params    string `json:"params" gorm:"size:255;comment:请求参数"`
	Status    uint32 `json:"status" gorm:"default:200;comment:状态码"`
	Method    string `json:"method" gorm:"size:7;default:GET;comment:访问方法"`
	ErrMsg    string `json:"errMsg" gorm:"column:errMsg; size:255;comment:报错信息"`

	AtTime utils.MyTime `json:"atTime" gorm:"column:atTime;type:datetime;comment:访问时间"`
}

// Menu 菜单
type Menu struct {
	Id   uint32 `json:"id"`
	Pid  uint32 `json:"pid" gorm:"default:0;comment:上级菜单"`
	Name string `json:"name" gorm:"size:10;comment:菜单名称"`
	Icon string `json:"icon" gorm:"size:20;comment:菜单图标"`
	Path string `json:"path" gorm:"size:50;comment:访问路径"`
}

// Resource 资源
type Resource struct {
	Id     uint32 `json:"id"`
	Method string `json:"method" gorm:"size:10;default:get;comment:允许访问的方法"`
	Path   string `json:"path" gorm:"size:50;comment:访问路径"`
	Name   string `json:"name" gorm:"size:20;comment:资源名称"`
	Params string `json:"params" gorm:"size:255;comment:请求参数"`
}

// User 用户
type User struct {
	Id       uint32 `json:"id"`
	UserName string `json:"userName" gorm:"column:userName;size:20;unique;comment:用户名" valid:"Required; MinSize(4); MaxSize(20);"`
	Password string `json:"password" gorm:"size:16;comment:登录密码" valid:"Required; Length(16);"`
	Name     string `json:"name" gorm:"size:10;comment:用户姓名" valid:"Required; MinSize(2); MaxSize(4); Match(\\p{Han}+);"`
	Phone    string `json:"phone" gorm:"size:11;comment:手机号码" valid:"Required; Mobile;"`
	Email    string `json:"email" gorm:"size:50;comment:邮箱" valid:"Required; Email;"`
	Status   bool   `json:"status" gorm:"default:true;comment:用户状态"`
	RoleId   uint32 `json:"roleId" gorm:"column:roleId;default:0;comment:用户角色" valid:"Required;"`
	Remark   string `json:"remark" gorm:"size:255;comment:备注"`

	AddTime utils.MyTime `json:"addTime" gorm:"column:addTime;type:datetime;comment:注册时间"`
}

// Role 角色
type Role struct {
	Id   uint32 `json:"id"`
	Name string `json:"name" gorm:"size:10;unique;comment:角色名称" valid:"Required; MinSize(2); MaxSize(10);"`
	Menu string `json:"menu" gorm:"size:255;comment:可见菜单" valid:"Required; MaxSize(255);"`
	Res  string `json:"res" gorm:"comment:可用资源"`
}

// 附加表名前缀
var p = "basic_"

func (Logger) TableName() string   { return p + "logger" }
func (Menu) TableName() string     { return p + "menu" }
func (Resource) TableName() string { return p + "resource" }
func (User) TableName() string     { return p + "user" }
func (Role) TableName() string     { return p + "role" }

func (model *User) BeforeCreate(tx *gorm.DB) error {
	model.AddTime = utils.MyTime{time.Now()} // 设置当前时间为创建时间
	return nil
}
